package com.webull.information.center.boot;

import java.util.Properties;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.core.type.AnnotatedTypeMetadata;

@Configuration
@Conditional(CarwlerUsStarter.NewsJobsCondition.class)
@ImportResource(locations = { "classpath:/webull-information-center/spring/carwler-us-app.xml" })
public class CarwlerUsStarter {

	@Conditional(NewsJobsCondition2.class)
	@ImportResource(locations = { "classpath:/webull-information-center/spring/carwler-us-elasticJob.xml" })
	public class WebullElasticJob {
	}


	public static class NewsJobsCondition implements Condition {
		protected final Logger logger = LogManager.getLogger(getClass());

	@Override
		public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
	 boolean res = false;
	 try {
	
	 Properties appProperties = EnvProp.getEnvProp();
	
	 String run = appProperties.getProperty("news.job.run");
	 logger.info("CarwlerUsStarter.NewsJobsCondition.matches	 run:{}.!!!!!!!!!!!!!!!!!!!", run);
	 res = "us".equalsIgnoreCase(run);
	 } catch (Exception e) {
	 logger.error("CarwlerUsStarter.NewsJobsCondition.matches	 error.!!!!!!!!!!!!!!!!!!!", e);
	 }
			logger.info("CarwlerUsStarter.NewsJobsCondition.matches	 result:{}.!!!!!!!!!!!!!!!!!!!", res);
	 return res;
	 }

	}

	public static class NewsJobsCondition2 implements Condition {
		protected final Logger logger = LogManager.getLogger(getClass());

	@Override
	 public boolean matches(ConditionContext context, AnnotatedTypeMetadata
	 metadata) {
	 boolean res = false;
	 try {
	 Properties appProperties = EnvProp.getEnvProp();
	
	 String run = appProperties.getProperty("news.job.run");
	 String run2 = appProperties.getProperty("news.job.run2", "true");
				logger.info("CarwlerUsStarter.NewsJobsCondition.matches  run:{}.!!!!!!!!!!!!!!!!!!!", run);
	 res = "us".equalsIgnoreCase(run) && "true".equals(run2);
	 } catch (Exception e) {
				logger.error("CarwlerUsStarter.NewsJobsCondition.matches  error.!!!!!!!!!!!!!!!!!!!", e);
	 }
			logger.info("CarwlerUsStarter.NewsJobsCondition.matches	 result:{}.!!!!!!!!!!!!!!!!!!!", res);
	 return res;
	 }

	}
}
